package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Resource
    private EmployeeService employeeService;

    @Resource
    private HttpServletRequest request;

    /**
     * 登录功能
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee){
        R<Employee> r = employeeService.login(employee);
        if(r.getCode() == 1){//代表登录成功
            //存储用户ID到session种
            HttpSession session = request.getSession();
            session.setAttribute("employee",r.getData().getId());
        }
        return r;
    }
    /**
     * 退出功能
     */
    @PostMapping("/logout")
    public R logout(){
        //todo 1.清空该用户的session就登录退出
        HttpSession session = request.getSession();
        session.removeAttribute("employee");
        return R.success("退出成功");
    }
}
